/**
* Copyright (c) 2015 
* All rights reserved.  
*  
* @file       gui_fillpolygon.c  
* @brief      Fill polygon routine
* @author  wangjj
* @Email    
* @date     2015-11-17 
* @version 1.0  
*/

#include <cdefBf531.h>
#include <ccblkfn.h>
#include <string.h>
#include "Assistant.h"
#include "DPX.h"
#include "gui.h"
#include "Icon.h"
#include "AcqView.h"

/**Define minimum and maximum coordinates in x and y ,used in fillpolygon*/
#define GUI_XMIN -4095
#define GUI_XMAX  4095
#define GUI_YMIN -4095
#define GUI_YMAX  4095


#define GUI_FP_MAXCNT 12

static int GL_FP_Cnt;
static short _aX[GUI_FP_MAXCNT];


/*********************************************************************
*
*       _DrawHLine
*/
static void _DrawHLine(int x0, int y,int x1,short color) 
{
  if (x0 < x1) {
    GUI_DrawHLine(x0, x1 ,y,color);
  } else {
    GUI_DrawHLine(x1,x0 ,y, color);
  }
}


/*********************************************************************
*
*       _CheckYInterSect
*
*  This function returns the x-coordinate of the intersection
*  of the given line at the given y-coordinate.
*  If there is no intersection, GUI_XMAX is returned.
*  This routine does not work for horizontal lines, as there
*  would be more than a single point as intersection. This situation
*  needs to be checked prior to calling the routine.
*  Returns:
*    0 if no intersection
*    1 if we have an intersection
*/
static int _CheckYInterSect(int y, int* px, const GUI_POINT*paPoint0, const GUI_POINT*paPoint1) 
{
  int x0, y0, x1, y1;
  if (paPoint0->y <= (paPoint1)->y) {
    y0 = paPoint0->y;
    if (y0 > y)      /* Check if there is an intersection ... (early out) */
      return 0;
    y1 = paPoint1->y;
    if (y1 < y)      /* Check if there is an intersection ... (early out) */
      return 0;
    x0 = paPoint0->x;
    x1 = paPoint1->x;
  } else {
    y0 = paPoint1->y;
    if (y0 > y)      /* Check if there is an intersection ... (early out) */
      return 0;
    y1 = paPoint0->y;
    if (y1 < y)      /* Check if there is an intersection ... (early out) */
      return 0;
    x0 = paPoint1->x;
    x1 = paPoint0->x;
  }
/* Calculate intersection */
  {
    int Mul = (int)(x1 - x0) * (int)(y - y0);
    if (Mul > 0) {
      Mul += (y1 - y0) >> 1;              /* for proper rounding */
    } else {
      Mul -= ((y1 - y0) >> 1) - 1;        /* for proper rounding */
    }
    x0 += Mul / (y1 - y0);
  }
  *px = x0;
  return 1;
} 



/*********************************************************************
*
*       _Add
*
*  This function adds a point into the sorted array
*/
static void _Add(int x) 
{
  if (GL_FP_Cnt < GUI_FP_MAXCNT) {
    int i;
    /* Move all entries to the right (bigger x-value) */
    for (i=GL_FP_Cnt; i ; i--) {
      if (_aX[i-1] < x)
        break;
      _aX[i] = _aX[i-1];
    }
    /* Insert new entry */
    _aX[i]    = x;
    GL_FP_Cnt++;
  }
}

/*********************************************************************
*
*       _Init
*
*  This function initialise the sorted array
*/
static void _Init(void) 
{
  GL_FP_Cnt = 0;
}

/*********************************************************************
*
*       _Flush
*
*  This function draw lines between points in the array
*/
static void _Flush(int x0, int y,short color) 
{
  int i, x = 0;
  char On=0;
  for (i=0; i<GL_FP_Cnt; i++) {
    int xNew = _aX[i];
    if (On) {
      GUI_DrawHLine(x0 + x, x0 + xNew, y,color);
    }
    On ^= 1;
    x = xNew;
  }
}

/*********************************************************************
*
*       _AddPoint
*
*  This function decides either if there a V-point or a
*  X-point. An X-point is added to the array, a V-point
*  is drawn.
*/
static void _AddPoint(int x, int y, int y0, int y1, int xOff, int yOff,short color) 
{
  if ((y0 ^ y1) >= 0) {
    x += xOff;
     GUI_SetPixel(x, y + yOff, color);   /* V-point, not crossing the polygon */
  } else {
    _Add(x);
  }
}


/*********************************************************************
*
*       _GetPrevPointDiffy
*
*  Find previous point which is not on the same height
*/
static int _GetPrevPointDiffy(const GUI_POINT* paPoint, int i,
                              const int NumPoints, const int y0) 
{
  int j, y1;
  for (j = 0; j < (NumPoints - 1) ; j++) {
    i = (i != 0) ? i - 1 : NumPoints - 1;
    y1 = (paPoint + i)->y;
    if (y1 != y0) {
      return y1;
    }
  }
  return y0;
}


/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       GUI_FillPolygon
*
*  This function calculates the polygon
*@param pPoint    polygon corner point
*@param NumPoints    polygon corner count
*@param xOff    polygon position, x offset from the first point
*@param yOff    polygon position, y offset from the first point
*/
void GUI_FillPolygon  (const GUI_POINT*pPoint, int NumPoints, int xOff, int yOff,short color) 
{
  int i, y;
  int yMin = GUI_YMAX;
  int yMax = GUI_YMIN;
/* First step : find uppermost and lowermost coordinates */
  for (i=0; i<NumPoints; i++) {
    y = (pPoint + i)->y;
    if (y < yMin)
      yMin = y;
    if (y > yMax)
      yMax = y;
  }
/* Second step: Calculate and draw horizontal lines */
  for (y=yMin; y<=yMax; y++) {
    _Init();
    /* find next intersection and count lines*/
    for (i=0; i<NumPoints; i++) {
      int i1 = (i < (NumPoints - 1)) ? i + 1 : 0;
      int y0 = (pPoint + i )->y;
      int y1 = (pPoint + i1)->y;
      /* Check if starting point is on line */
      if (y0 == y) {
        if (y1 == y) {  /* Add the entire line */
          _DrawHLine((pPoint + i )->x + xOff , y + yOff, (pPoint + i1)->x + xOff,color);
        } else {        /* Add only one point */
          int yPrev = _GetPrevPointDiffy(pPoint, i, NumPoints, y);
          if (yPrev != y) {
            _AddPoint((pPoint + i)->x, y, yPrev - y, y1 - y, xOff, yOff,color);
          } 
        }
      } else if (y1 != y) {  /* Ignore if end-point is on the line */
        if (((y1 >= y) && (y0 <= y)) || ((y0 >= y) && (y1 <= y))) {
          int xIntersect;
          if (_CheckYInterSect(y, &xIntersect, pPoint + i, pPoint + i1)) {
            _Add(xIntersect);
          }
        }
      }
    }
    _Flush(xOff, y + yOff,color);
  }  
}


